package org.provider.rabbitmq.support;

import java.util.UUID;

import org.common.enums.ExchangeEnum;
import org.common.enums.QueueEnum;
import org.provider.rabbitmq.QueueMessageService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
*@author LSY
*Time: 2018年12月28日下午6:12:28
*
*
*/
@Component
public class QueueMessageServiceSupport implements QueueMessageService{

	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause) {
		System.out.println("回调id:" + correlationData.getId());
		if(ack) {
			System.out.println("发送信息成功");
		}else {
			System.out.println("发送信息失败：" + cause);
		}
	}

	@Override
	public void send(Object msg, ExchangeEnum exchangeEnum, QueueEnum queueEnum) {
		//回调当前类
		rabbitTemplate.setConfirmCallback(this);
		//构建回调id
		CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
		
		rabbitTemplate.convertAndSend(exchangeEnum.getValue(), queueEnum.getRootingKey(), msg, correlationId);
		
	}

}
